package fag.editor;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.event.EventListenerList;

public class ModeChooser extends JPanel implements ActionListener {

	private static final long serialVersionUID = 3203920097034108280L;
	
	private final static int DEFAULT_WIDTH = 120;
	private final static int DEFAULT_HEIGHT = 75;
	
	private JToggleButton tbtSprite = new JToggleButton(new ImageIcon(getClass().getResource("/imgs/sprite.png"),"Sprite"));
	private JToggleButton tbtHitbox = new JToggleButton(new ImageIcon(getClass().getResource("/imgs/shape.png"), "HitboxIcon"));
	
	protected final static String ACTION_CHANGED_MODE = "Mode : Changed";
	
	private final EventListenerList listeners = new EventListenerList();
	
	private String currentMode = "none";
	
	
	public ModeChooser() {
		
		setBorder(BorderFactory.createTitledBorder("Mode"));
		
		add(tbtHitbox);
		add(tbtSprite);
		
		tbtHitbox.setPreferredSize(new Dimension(40,40));
		tbtSprite.setPreferredSize(new Dimension(40,40));
		
		tbtSprite.addActionListener(this);
		tbtHitbox.addActionListener(this);
		
		repaint();
	}
	
	public String getCurrentMode() {
		
		return currentMode;
	}
	
	public void resetSelection() {
		
		tbtSprite.setSelected(false);
		tbtHitbox.setSelected(false);
	}
	
	@Override
	public Dimension preferredSize() {
		
		return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT);
	}
	
	public void addActionListener(ActionListener listener) {
		
		listeners.add(ActionListener.class,listener);
	}
	
	public void removeActionListener(ActionListener listener) {
		
		listeners.remove(ActionListener.class, listener);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {

		if(e.getSource() == tbtSprite) {
		
			tbtSprite.setSelected(true);
			tbtHitbox.setSelected(false);
			currentMode = "Sprite";
			currentModeChanged();
		}
		
		if(e.getSource() == tbtHitbox) {
			
			tbtSprite.setSelected(false);
			tbtHitbox.setSelected(true);
			currentMode = "Hitbox";
			currentModeChanged();
		}
	}
	
	protected void currentModeChanged() {
		
		for(ActionListener listener : listeners.getListeners(ActionListener.class)) 
			listener.actionPerformed(new ActionEvent(this,ActionEvent.ACTION_LAST + 1,ACTION_CHANGED_MODE));
	}
}
